<template>
  <div class="back">
    <div class="register">
      <el-form :model="userInfor"
               status-icon
               :rules="rules"
               ref="userInfor"
               label-width="100px"
               class="demo-userInfor">
        <h3 class="title">注册页面</h3>
        <el-form-item label="手机号："
                      prop="phone">
          <el-input type="text"
                    v-model="userInfor.phone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称："
                      prop="username">
          <el-input type="text"
                    v-model="userInfor.username"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码："
                      prop="password">
          <el-input type="password"
                    v-model="userInfor.password"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码："
                      prop="checkPass">
          <el-input type="password"
                    v-model="userInfor.checkPass"
                    autocomplete="off"></el-input>
        </el-form-item>
        <div class="tologin">
          <router-link to="login">前往登录>>></router-link>
        </div>
        <el-button class="elButton"
                   type="primary"
                   :loading="loading"
                   :disabled="loading"
                   @click="submitForm('userInfor')">{{buttonText}}</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data () {
    var checkPhone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('手机号不能为空'))
      } else {
        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
        console.log(reg.test(value))
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入正确的手机号'))
        }
      }
    }
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('昵称不能为空'))
      } else {
        const reg = /^[a-zA-Z0-9_-]{4,16}$/
        console.log(reg.test(value))
        if (reg.test(value)) {
          callback()
        } else {
          return callback(new Error('请输入字数在4~16位不包含特殊字符的昵称'))
        }
      }
    }
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.userInfor.checkPass !== '') {
          this.$refs.userInfor.validateField('checkPass')
        }
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.userInfor.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      buttonText: '注册',
      loading: false,
      userInfor: {
        phone: '',
        username: '',
        password: '',
        checkPass: ''
      },
      rules: {
        phone: [
          { validator: checkPhone, trigger: 'blur' }
        ],
        username: [
          { validator: checkUsername, trigger: 'blur' }
        ],
        password: [
          { validator: validatePass, trigger: 'blur' }
        ],
        checkPass: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      var _this = this
      this.loading = true
      this.buttonText = '提交中...'
      this.$refs[formName].validate((valid) => {
        var getUser = {
          username: this.userInfor.username,
          password: this.userInfor.password
        }
        console.log(getUser)
        this.$axios({
          url: '/user/register',
          method: 'post',
          dataType: 'json',
          data: getUser
        }).then(successResponse => {
          if (successResponse.data.code === 200) {
            this.$message.success('注册成功')
            setTimeout(function () {
              _this.$router.push({ name: 'login' })
            }, 1000)
          } else {
            this.$message.error('注册失败')
            this.loading = false
            this.buttonText = '注册'
          }
          console.log(successResponse)
        }).catch(failResponse => {
          console.log(failResponse)
          var errorStr = failResponse.toString()
          this.$message.error(errorStr)
          this.loading = false
          this.buttonText = '注册'
        })
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';

.back {
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  height: 100%;
  background-image: $backImage;
  background-size: cover;
}

.register {
  background: #fff;
  padding: 0.5rem;
  width: 10rem;
  border-radius: 0.4rem;
  text-align: center;

  .title {
    font-weight: bold;
    font-size: 0.4rem;
    margin: 0 auto 30px;
    text-align: center;
    color: #707070;
  }

  .tologin {
    float: right;
    padding-bottom: 0.3rem;
  }

  .elButton {
    width: 100%;
  }
}
</style>
